/*
 * Copyright (C),2015,北京新诺创科软件技术有限公司
 * author zhangmengliang
 */
package com.xnck.example.jertest.util;


import com.xnck.example.jertest.constant.ContextConstant;
import com.xnck.example.jertest.context.HttpContext;

import javax.servlet.http.HttpSession;

/**
 * 当前登录用户信息
 */
public class UserUtil {

    /**
     * 获得当前登录人员的凭证信息
     */
    public static String getId(){
        HttpSession session = HttpContext.get().getSession();
        Object obj = null;
        if (null != session){
            obj = session.getAttribute(ContextConstant.IDEN_CERT_KEY);
        }
        if (null == obj) {
            return null;
        }
        return obj.toString();
    }

    /**
     * 设置当前登录人员的凭证信息
     */
    public static void setId(String id) throws Exception {
        HttpSession session = HttpContext.get().getRequest().getSession(true);
        session.setAttribute(ContextConstant.IDEN_CERT_KEY, id);
    }

    /**
     * 移除当前登录人员的凭证信息
     */
    public static void removeId() throws Exception {
        HttpSession session = HttpContext.get().getSession();
        if (null != session){
            session.removeAttribute(ContextConstant.IDEN_CERT_KEY);
        }
    }
}
